<template>
  <div class='aui-card--fill scroll-box card-box'>
    <div class='pane-shell'>
      <search @sumbit='sumbit'>
        <!--  搜索项 -->
        <template slot='form'>
          <el-form :inline='true'
                   @submit.native.prevent
                   :model='dataForm'
                   ref='dataForm'>
            <el-form-item>
              <el-select v-model="dataForm.type"
                         clearable
                         placeholder="请选择类型">
                <el-option v-for="item in typeList"
                           :key="item.dictValue"
                           :label="item.dictName"
                           :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop='label'
                          :rules="stringRule(false,0,50)">
              <el-input v-model.trim='dataForm.label'
                        class='form-item'
                        clearable
                        placeholder='请输入标签'></el-input>
            </el-form-item>
            <el-form-item>
              <el-select v-model="dataForm.fileId"
                         clearable
                         placeholder="请选择法律文件">
                <el-option v-for="item in legalOptions"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop='content'
                          :rules="stringRule(false,0,500)">
              <el-input v-model='dataForm.content'
                        class='form-item'
                        placeholder='请输入法律内容'></el-input>
            </el-form-item>
          </el-form>
        </template>
        <!--  操作按钮 -->
        <template slot='btns'>
          <el-button type="primary"
                     v-if="$hasPermission('tb:law:getLawFileAll')"
                     @click="openDialog('LegalDocumentSettings')">法律文件设置</el-button>
          <el-button type="primary"
                     plain
                     icon="el-icon-plus"
                     v-if="$hasPermission('tb:law:addLaw')"
                     @click="openDialog('addOrUpdate',{list:legalOptions})">{{ $t('add') }}法律</el-button>
        </template>
      </search>
      <table-template :tableData='tableData'
                      :loading='dataListLoading'
                      :fieldManagement='false'
                      :dataList='dataList'>
        <template slot="createName-before">
          <el-table-column prop="content"
                           align="center"
                           min-width="200"
                           label="法律内容">
            <template slot-scope="scope">
              <el-popover placement="bottom-start"
                          width="400"
                          trigger="hover">
                <div v-html="scope.row.content"></div>
                <div slot="reference"
                     class="overLine">{{matchReg(scope.row.content)}}</div>
              </el-popover>
            </template>
          </el-table-column>
        </template>
        <el-table-column :label="$t('handle')"
                         fixed='right'
                         header-align='center'
                         align='center'
                         v-if='dataList.length'
                         width='150'>
          <template slot-scope='scope'>
            <el-button type='text'
                       class='brand'
                       v-if="$hasPermission('tb:law:updateLaw')"
                       @click="openDialog('addOrUpdate',{id:scope.row.id,list:legalOptions})"
                       size='small'>修改</el-button>
            <el-button type='text'
                       size='small'
                       class='danger'
                       v-if="$hasPermission('tb:law:delLaw')"
                       @click='deleteHandle({id:scope.row.id})'>{{ $t('delete')}}</el-button>
          </template>
        </el-table-column>
      </table-template>
      <el-pagination :current-page='page'
                     :page-sizes='[10,20,50,100]'
                     :total='total'
                     layout='total, sizes, prev, pager, next'
                     @size-change='pageSizeChangeHandle'
                     @current-change='pageCurrentChangeHandle'>
      </el-pagination>
    </div>
    <legal-list-add-or-update ref="addOrUpdate"
                              @callBack="getDataList" />
    <legal-document-settings ref="LegalDocumentSettings" @callBack="getLawFileAll()"/>
  </div>
</template>
<script>
import { clearHTML } from '@/utils'
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import tableTemplate from '@/components/table-template'
import Search from '@/components/search/search'
import { mapState } from 'vuex'
import LegalListAddOrUpdate from './legal-list-add-or-update.vue'
import LegalDocumentSettings from './legal-document-settings.vue'
export default {
  mixins: [mixinViewModule, minFormValidate],
  name: '',
  data () {
    return {
      mixinViewModuleOptions: {
        getLawFileAllURL: '/tb/law/getLawFileAll',
        // getDataListURL: '/tb/law/getLawPage', // 数据列表接口，API地址
        getDataListURL: '',
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addOrUpdateURL: '', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '/tb/law/delLaw', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      legalOptions: [],
      dataList: [],
      dataForm: {}
    }
  },
  components: { tableTemplate, Search, LegalListAddOrUpdate, LegalDocumentSettings },
  computed: {
    ...mapState({
      typeList: state => state.dict.typeList
    }),
    tableData () {
      return [
        { label: '排序', width: '30', prop: 'sort' },
        { label: '类型', width: '30', prop: 'type', selectList: this.typeList },
        { label: '标签', width: '50', prop: 'label' },
        { label: '法律文件', width: '40', prop: 'fileName' },
        // { label: '法律内容', width: '80', prop: 'content' },
        { label: '创建人', width: '30', prop: 'createName' },
        { label: '创建时间', width: '50', prop: 'createDate' },
        { label: '更新人', width: '30', prop: 'updateName' },
        { label: '更新时间', width: '50', prop: 'updateDate' }
      ]
    }
  },
  mounted () {
    this.getLawFileAll()
  },
  methods: {
    sumbit () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.getPageInit()
        }
      })
    },
    matchReg (src) {
      if (!src) {
        return
      }
      return clearHTML(src)
    },
    getLawFileAll () {
      this.getData({ url: this.mixinViewModuleOptions.getLawFileAllURL }).then(res => {
        if (res.code !== 0) {
          return false
        }

        this.legalOptions.splice(0, this.legalOptions.length, ...res.data)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
